﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Threading.Tasks;
using System.IO;
using System.Reflection;
using System.Windows.Forms;

namespace Snake_Classic
{
    public class Snake
    {
        static string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
        string Filepath = Path.Combine(directory, "segment.png");
        string Filepath2 = Path.Combine(directory, "head.png");
        private Rectangle[] snakeRec;
        //snakeRec propertie
        public Rectangle[] SnakeRec
        {
            get
            {
                return snakeRec;
            }
        }

        private int x, y, width, height;
        /// <summary>
        /// snake constructure
        /// </summary>
        public Snake()
        {
            snakeRec = new Rectangle[2]; 
            x = 20;
            y = 0;
            width = 20;
            height = 20;
            for (int i = 0; i < snakeRec.Length; i++)
            {
                 snakeRec[i] = new Rectangle(x, y, width, height);
                 x -= 20;
            }
        }
        /// <summary>
        /// draw snake on a form
        /// </summary>
        /// <param name="graphics"></param>
        public void DrawSnake(Graphics graphics)
        {
            //create image from path
            Image segmentSnakeRec = Image.FromFile(Filepath);
            Image headSnakeRec = Image.FromFile(Filepath2);
            for (int i = 0; i < snakeRec.Length; i++)
            {
                Brush snake_color = i == 0 ? Brushes.DarkBlue : Brushes.Green;//snake head will be blue and body green
                if (i == 0)
                {
                    graphics.DrawImage(headSnakeRec, snakeRec[0]);
                }
                else
                {
                    graphics.DrawImage(segmentSnakeRec, snakeRec[i]);
                }
            }
        }
        public void DrawSnake()
        {
            for (int i = snakeRec.Length - 1; i > 0; i--)
            {
                snakeRec[i] = snakeRec[i - 1];
            }
        }
        /// <summary>
        /// methods form move snake on a field
        /// </summary>
        public void MoveDown()
        {
            DrawSnake();
            snakeRec[0].Y += 20;
        }
        public void MoveUp()
        {
            DrawSnake();
            snakeRec[0].Y -= 20;
        }
        public void MoveRight()
        {           
            DrawSnake();
            snakeRec[0].X += 20;
           
        }
        public void MoveLeft()
        {
            DrawSnake();
            snakeRec[0].X -= 20;
        }

        /// <summary>
        /// snake grows when she is eating a food 
        /// </summary>
        public void GrowSnake()
        {
            List<Rectangle> rec = snakeRec.ToList();
            rec.Add(new Rectangle(snakeRec[snakeRec.Length - 1].X, snakeRec[snakeRec.Length - 1].Y, width, height));
            snakeRec = rec.ToArray();
        }
    }
}
